<?php
// Fill up array with names

include 'config.php';

$conn = mysql_connect($host, $user, $password) or die(mysql_error());
mysql_select_db("mweb_survey");

//get the q parameter from URL
$q=$_GET["q"];

$sql = "SELECT * FROM company WHERE CompID like '%$q%'";
$result = mysql_query($sql, $conn) or die(mysql_error());

while ($row = mysql_fetch_assoc($result)) {
	$a[] = $row['CompID'];
}

//lookup all hints from array if length of q>0
$response = "";

if (strlen($q) > 1)
{
  $hint="";
  if (isset($a)) {
  for($i=0; $i<count($a); $i++)
    {
    // if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
      // {
      if ($hint=="")
        {
        $hint=$a[$i];
        }
      else
        {
        $hint=$hint."<br/>".$a[$i];
        }
      // }
    }
  }
  
  if ($hint == "")
  {
  $response="no suggestion";
  }
else
  {
  $response=$hint;
  }
}

// Set output to "no suggestion" if no hint were found
// or to the correct values
// if ($hint == "")
  // {
  // $response="no suggestion";
  // }
// else
  // {
  // $response=$hint;
  // }

//output the response
echo $response;
?>